const maxPage = 4;
var curPage = 1;
//app的配置信息
const data = {};

function init() {
    debugger
    $('.cp-nPage').on('click', apiNextPage.bind(this));
    $('.cp-lPage').on('click', apiLastPage.bind(this));

    $(function () {
        isCreateHTTPS();
        isCreateHTTP();
        isCreateCORS();
    });

    //是否开启HTTPS服务
    $("#is-create-https").click(function () {
        isCreateHTTPS();
    });
    //是否开启HTTP服务
    $("#is-create-http").click(function () {
        isCreateHTTP();
    });
    //是否开启跨域
    $("#is-create-cors").click(function () {
        isCreateCORS();
    });
    //notFoundContentType 找不到路径(404) Content-Type:下拉框改变事件
    $("#notFoundContentType").change(function () {
        if ($(this).val() == "custom") {
            $("#notFoundContentTypeCustom").show();
        } else {
            $("#notFoundContentTypeCustom").hide();
        }
    });
    //blacklistIpContentType 在黑名单列表中返回Content-Type:下拉框改变事件
    $("#blacklistIpContentType").change(function () {
        if ($(this).val() == "custom") {
            $("#blacklistIpContentTypeCustom").show();
        } else {
            $("#blacklistIpContentTypeCustom").hide();
        }
    });
}
// 下一页
function apiNextPage(ev) {
    curPage = parseInt($('#apiListModal').attr('cur'));
    if(curPage === 1){
        if (!basicInfoCheck()){
            return;
        }
    }
    if(curPage === 2){
        if (!serverInfoCheck()){
            return;
        }
    }
    if(curPage === 3){
    	if (!resultInfoCheck()){
    		return;
    	}
    }

    if (curPage <= 5) {
        $('.createApp' + curPage).addClass('hide');
        // 导航
        $('.cApiNav' + curPage).removeClass('guidebar-current');
        $('#apiListModal').attr('cur', ++curPage);
        var newCur = $('#apiListModal').attr('cur');
        $('.createApp' + newCur).removeClass('hide');
        $('.cApiNav' + newCur).addClass('guidebar-current');
    }
    if (curPage === 4) {
        $('.cp-nPage').addClass('hide');
        $('.cp-sPage').removeClass('hide');
    } else {
        $('.cp-nPage').removeClass('hide');
        $('.cp-sPage').addClass('hide');
    }
    if (curPage > 1) {
        $('.cp-lPage').removeClass('hide');
    } else {
        $('.cp-lPage').addClass('hide');
    }

    if (curPage === 3) {
        $('.bParam-config tbody tr').not(':first').remove();
    }
}
// 上一页
function apiLastPage(ev) {
    curPage = parseInt($('#apiListModal').attr('cur'));
    if (curPage >= 2) {
        $('.createApp' + curPage).addClass('hide');
        $('.cApiNav' + curPage).removeClass('guidebar-current');
        $('#apiListModal').attr('cur', --curPage);
        var newCur = $('#apiListModal').attr('cur');
        $('.createApp' + newCur).removeClass('hide');
        $('.cApiNav' + newCur).addClass('guidebar-current');
    }
    if (curPage === 4) {
        $('.cp-sPage').removeClass('hide');
    } else {
        $('.cp-sPage').addClass('hide');
        $('.cp-nPage').removeClass('hide');
    }
    if (curPage > 1) {
        $('.cp-lPage').removeClass('hide');
    } else {
        $('.cp-lPage').addClass('hide');
    }
}

//基本信息检查
function basicInfoCheck() {
    data.appName = $("#app-name").val();
    if (data.appName == '') {
        $.showNotice("ERROR","请输入应用名称");
        return false;
    }
    data.describe = $("#app-describe").val();
    if (data.describe == '') {
        $.showNotice("ERROR","请输入应用描述");
        return false;
    }
    data.scope = parseInt($('input:radio[name="scope"]:checked').val());
    return true;
}
//信息定义检查
function resultInfoCheck() {
	  if ($("#notFoundContentType").val() != 'custom') {
	    	data.notFoundContentType = $("#notFoundContentType").val();
	  }else{
		  data.notFoundContentType = $("#notFoundContentTypeCustom").val();
	  }
	  if ($("#notFoundResult").val() != '') {
		 data.notFoundResult = $("#notFoundResult").val();
	  }
	  
	  if ($("#blacklistIpCode").val() != '') {
		 var code= parseInt($("#blacklistIpCode").val());
		  if(isNaN(code)){
			  $.showNotice("ERROR",'状态码必须为数字');
			  return false;
		  }
		  data.blacklistIpCode = code;
	  }
	  
	  if ($("#blacklistIpContentType").val() != 'custom') {
	    	data.blacklistIpContentType = $("#blacklistIpContentType").val();
	  }else{
		  data.blacklistIpContentType = $("#blacklistIpContentTypeCustom").val();
	  }
	  if ($("#blacklistIpResult").val() != '') {
		  data.blacklistIpResult =$("#blacklistIpResult").val();
	  }
	return true;
}

//服务器信息配置检查
function serverInfoCheck() {
    var serverOptions = {};
    serverOptions.createHttp = $("#is-create-http").is(":checked");
    serverOptions.createHttps = $("#is-create-https").is(":checked");
    serverOptions.httpPort = parseInt($("#http-port").val());
    serverOptions.httpsPort = parseInt($("#https-port").val());
    data.serverOptions = serverOptions;
    if (!(serverOptions.createHttp || serverOptions.createHttps)) {
        $.showNotice("ERROR","必须最少选择一种服务HTTP或者HTTPS");
        return false;
    }
    if (serverOptions.createHttps) {
        var cert_type = $('input:radio[name="cert-type"]:checked')
            .val();
        if (cert_type == null || cert_type == '') {
            $.showNotice("ERROR","请选择证书类型!");
            return false;
        }
        var cert_key = $("#cert-key").val();
        if (cert_key == '') {
            $.showNotice("ERROR","请输入证书的key值或者路径");
            return false;
        }
        var cert_path = $("#cert-path").val();
        if (cert_path == '') {
            $.showNotice("ERROR","请输入证书所在路径");
            return false;
        }
        var certOptions ={};
        certOptions.certType = cert_type;
        certOptions.certKey = cert_key;
        certOptions.certPath = cert_path;
        serverOptions.certOptions = certOptions;
    }
    if ($("#is-create-cors").is(":checked")) {
        var corsOptions = {};
        corsOptions.allowedOrigin = $("#allowedOrigin").val();
        if (corsOptions.allowedOrigin == '') {
            $.showNotice("ERROR","跨域请求请输入允许请求源,如果运行所有可以输入*号");
            return false;
        }
        corsOptions.allowCredentials = $(
            'input:radio[name="allowCredentials"]:checked').val();
        if(corsOptions.allowCredentials == "true" && "*" == corsOptions.allowedOrigin){
        	$.showNotice("ERROR","跨域请求源,如果为*,allowCredentials不允许为true,需要非*才可以为true");
        	return false;
        }
        corsOptions.maxAgeSeconds = parseInt($("#maxAgeSeconds").val());
        var ah = $("#allowedHeaders").val();
        if (ah != '') {
            ah = ah.replace("，", ",");
            var item=ah.split(",");
            var items=[];
            for(var i=0;i<item.length;i++){
                if(item[i] != ''){
                    items.push(item[i]);
                }
            }
            if (items.length>0){
                corsOptions.allowedHeaders =items;
            }
        }
        var eh = $("#exposedHeaders").val();
        if (eh != '') {
            eh = eh.replace("，", ",");
            var item=eh.split(",");
            var items=[];
            for(var i=0;i<item.length;i++){
                if(item[i] != ''){
                    items.push(item[i]);
                }
            }
            if (items.length>0){
                corsOptions.exposedHeaders =items;
            }
        }
        var method = new Array();
        var item = $('input[name="allowedMethods"]');
        for (var i = 0; i < item.length; i++) {
            if ($(item[i]).is(":checked")) {
                method.push($(item[i]).val())
            }
        }
        corsOptions.allowedMethods = method;
        data.corsOptions = corsOptions;
    }
    return true;
}
//应用信息检查
function appConfigCheck() {
    if($("#custom").val() != ''){
        if(!isJSON($("#custom").val())){
            $.showNotice("ERROR","拓展配置必须为JSON格式");
            return false;
        }else{
            data.serverOptions.custom=$("#custom").val();
        }
    }
    if($("#webClientCustom").val() != ''){
    	if(!isJSON($("#webClientCustom").val())){
    		$.showNotice("ERROR","WebClient拓展配置必须为JSON格式");
    		return false;
    	}else{
    		data.webClientCustom=$("#webClientCustom").val();
    	}
    }
    if ($("#contentLength").val() != '') {
        data.contentLength = parseInt($("#contentLength").val());
    }
    if ($("#sessionTimeOut").val() != '') {
        data.sessionTimeOut = parseInt($("#sessionTimeOut").val());
    }
    if ($("#sessionCookieName").val() != '') {
        data.sessionCookieName = $("#sessionCookieName").val();
    }
    data.keepAlive = $('input:radio[name="keepAlive"]:checked').val();
    if ($("#decoderInitialBufferSize").val() != '') {
    	data.decoderInitialBufferSize = parseInt($("#decoderInitialBufferSize").val());
    }
    if ($("#maxPoolSize").val() != '') {
        data.maxPoolSize = parseInt($("#maxPoolSize").val());
    }
    if ($("#maxInitialLineLength").val() != '') {
        data.maxInitialLineLength = parseInt($("#maxInitialLineLength").val());
    }
    if ($("#maxHeaderSize").val() != '') {
        data.maxHeaderSize = parseInt($("#maxHeaderSize").val());
    }
    return true;
}


function isCreateHTTPS() {
    if ($("#is-create-https").is(":checked")) {
        $(".https-hide-show").show();
    } else {
        $(".https-hide-show").hide();
    }
}

function isCreateHTTP() {
    if ($("#is-create-http").is(":checked")) {
        $(".http-hide-show").show();
    } else {
        $(".http-hide-show").hide();
    }
}

function isCreateCORS() {
    if ($("#is-create-cors").is(":checked")) {
        $(".cors-hide-show").show();
    } else {
        $(".cors-hide-show").hide();
    }
}


//确定创建
function saveApp() {
    if (data.action === "UPDATE") {
        // 第1页
        if (!basicInfoCheck()) {
            return;
        }
        // 第2页
        if (!serverInfoCheck()) {
            return;
        }
        // 第3页
        if (!resultInfoCheck()) {
            return;
        }
        // 第4页
        if(!appConfigCheck()){
            return;
        }
    }
    data.time = $("#time").val()
    debugger
    $.ajax({
        type : "post",
        url : data.url,
        async : true,
        data : JSON.stringify(data),
        dataType : "json",
        success : function(result) {
            if (result.code === 200) {
                // $.showNotice("ERROR","创建成功");
                window.location.href = "/static/app/list";
            } else {
                $.showNotice("ERROR:" + result.code, "创建应用失败:" + result.msg);
            }
        },
        error : function() {
            $.showNotice("ERROR","创建应用失败");
        }
    });
}

// 检查参数是否为json格式
function isJSON(str) {
    if (typeof str != 'string') {
        return false;
    }
    try {
        const obj = JSON.parse(str);
        // if (typeof obj == 'object' && obj) {
        //     return true;
        // } else {
        //     return false;
        // }
        return (typeof obj == 'object' && obj);
    } catch (e) {
        console.log('error：' + str + '!!!' + e);
        return false;
    }
}